<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div>
        hello
    </div>
    <input type="text" id="content">
    <button onclick="send()">发言</button>
    <!-- node_modules中 -->
    <script src="/socket.io/socket.io.js"></script>
    <script>
        let inputContent = document.querySelector("#content")
        // http://www.zhufengpeixun.com/plan/html/36.websocket-2.html
        // 引入以上js文件后，即可拿到 io方法
        let socket = io('/dev') // new Websocket("ws://localhost:xxxx")
        // let socket = io.connect();
        // let socket = io.connect('/');
        // let socket = ip.connect('localhost:xxxx/')

        socket.on('connect',function() {
            console.log('连接成功');
            socket.send('服务器你好');
            // socket.emit('message','服务器你好');
        })
        // 监听服务器发过来的消息
        socket.on('message', function (message) {
            console.log(message)
        })
        socket.on('error', function (err) {
            console.log(err, 'err')
        })
        //
        function send() {
            let val = inputContent.value;
            socket.send(val);
        }
    </script>
</body>
</html>